//
//  UIView+Extensions.m
//  TLKit
//
//  Created by 李伯坤 on 2017/8/27.
//  Copyright © 2017年 libokun. All rights reserved.
//

#import "UIView+Extensions.h"
#import <objc/runtime.h>

static NSString *kStringTag = @"kStringTag";

@implementation UIView (Extensions)

- (UIViewController *)viewController
{
    for (UIView *next = self; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

- (UINavigationController *)navigationController
{
    return self.viewController.navigationController;
}

- (BOOL)isShowInScreen
{
    if (self == nil) {
        return NO;
    }
    
    // 转换view对应window的Rect
    CGRect rect = [self convertRect:self.bounds toView:nil];
    if (CGRectIsEmpty(rect) || CGRectIsNull(rect)) {
        return NO;
    }
    
    if (self.hidden ||                              // view已隐藏
        self.superview == nil ||                    // 没有父视图
        CGSizeEqualToSize(rect.size, CGSizeZero)    // size为0
        ) {
        return NO;
    }
    
    // 计算view与window交叉的rect
    CGRect screenRect = [UIScreen mainScreen].bounds;
    CGRect intersectionRect = CGRectIntersection(rect, screenRect);
    if (CGRectIsEmpty(intersectionRect) || CGRectIsNull(intersectionRect)) {
        return NO;
    }
    
    return YES;
}

- (void)removeAllSubviews
{
    for (id view in self.subviews) {
        [view removeFromSuperview];
    }
}

- (id)findSubViewWithClass:(Class)subViewClass
{
    for (id subView in self.subviews) {
        if ([subView isKindOfClass:subViewClass]) {
            return subView;
        }
    }
    
    return nil;
}

- (id)findSuperViewWithClass:(Class)superViewClass
{
    if (self == nil || self.superview == nil) {
        return nil;
    }
    else if ([self.superview isKindOfClass:superViewClass]) {
        return self.superview;
    }
    else {
        return [self.superview findSuperViewWithClass:superViewClass];
    }
}

- (UIView *)findFirstResponder
{
    if ([self canBecomeFirstResponder]  && self.isFirstResponder) {
        return self;
    }
    
    for (UIView *view in self.subviews) {
        UIView *firstResponder = [view findFirstResponder];
        if (firstResponder) {
            return firstResponder;
        }
    }
    return nil;
}

- (NSString *)stringTag {
    return objc_getAssociatedObject(self, &kStringTag);
}

- (void)setStringTag:(NSString *)tag {
    objc_setAssociatedObject(self, &kStringTag, tag, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}



#pragma mark - 快速添加阴影
- (void)addProjectionWithShadowOpacity:(CGFloat)shadowOpacity {
    self.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
    self.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,x向右偏移2，y向下偏移6，默认(0, -3),这个跟shadowRadius配合使用
    self.layer.shadowOpacity = shadowOpacity;//阴影透明度，默认0
    self.layer.shadowRadius = 3;//阴影半径，默认3
}
- (void)addBorderWithWidth:(CGFloat)width {
    self.layer.borderWidth = width;
    self.layer.borderColor = [UIColor blackColor].CGColor;
}
- (void)addBorderWithWidth:(CGFloat)width borderColor:(UIColor *)borderColor {
    self.layer.borderWidth = width;
    self.layer.borderColor = borderColor.CGColor;
}
- (void)addRoundedCornersWithRadius:(CGFloat)radius {
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
}
- (void)addRoundedCornersWithRadius:(CGFloat)radius byRoundingCorners:(UIRectCorner)corners{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}
- (void)addCornerBorderWithApplyRoundCorners:(UIRectCorner)corners
                                         radius:(CGFloat)radius
                                    strokeColor:(UIColor *)strokeColor
                                      lineWidth:(CGFloat)lineWidth{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
    
    CAShapeLayer *temp = [CAShapeLayer layer];
    temp.lineWidth = lineWidth;
    temp.fillColor = [UIColor clearColor].CGColor;
    temp.strokeColor = strokeColor.CGColor;
    temp.frame = self.bounds;
    temp.path = path.CGPath;
    [self.layer addSublayer:temp];
    
    CAShapeLayer *mask = [[CAShapeLayer alloc]initWithLayer:temp];
    mask.path = path.CGPath;
    self.layer.mask = mask;
}

- (void) addDashedLineBorderWithlineColor: (UIColor *) lineC {
    
    CAShapeLayer *layer   = [[CAShapeLayer alloc] init];
    layer.frame            = self.bounds;
    layer.backgroundColor   = [UIColor clearColor].CGColor;

    UIBezierPath *path    = [UIBezierPath bezierPathWithRoundedRect:layer.frame cornerRadius:4.0];
    layer.path             = path.CGPath;
    layer.lineWidth         = 1.0;//4.0f;   //虚线高度
    layer.lineDashPattern    = @[@5, @5];   //虚线长度和间隔
    layer.fillColor          = [UIColor clearColor].CGColor;
    layer.strokeColor       = lineC.CGColor;//[UIColor whiteColor].CGColor;
    
    [self.layer addSublayer:layer];
}


- (void)addGradientColorFromColors:(NSArray*)colorArr locationArr:(NSArray*) locationArr withGradientType:(GradientType)gradientType {
    
    CAGradientLayer *leftGradientLayer = [CAGradientLayer layer];
    leftGradientLayer.frame = self.bounds;
    [leftGradientLayer setColors:colorArr];
    [leftGradientLayer setLocations:locationArr];
    if (gradientType == GradientTypeTopToBottom) {
        [leftGradientLayer setStartPoint:CGPointMake(0, 0)];
        [leftGradientLayer setEndPoint:CGPointMake(0, 1)];
    } else if (gradientType == GradientTypeLeftToRight) {
        [leftGradientLayer setStartPoint:CGPointMake(0, 0)];
        [leftGradientLayer setEndPoint:CGPointMake(1, 0)];
    } else if (gradientType == GradientTypeUpleftToLowright) {
        [leftGradientLayer setStartPoint:CGPointMake(0, 0)];
        [leftGradientLayer setEndPoint:CGPointMake(1, 1)];
    } else if (gradientType == GradientTypeUprightToLowleft) {
        [leftGradientLayer setStartPoint:CGPointMake(1, 0)];
        [leftGradientLayer setEndPoint:CGPointMake(0, 1)];
    }
    
    
    [self.layer addSublayer:leftGradientLayer];
}


@end
